module loader.HmeLame;

import loader.Cfg;

import maths.Vec;
//import maths.Quat;

import utils.Memory;

import Mesh;

import std.stdio;

class HmeLame{

	void load(char[] filename){
		auto hme = (new CfgLoader).load(filename).result();
		mesh = new Mesh;
		writefln("Loading: ", filename);
		writefln("\tNode name: ", hme.child(`node`).string_(`name`));
		vec3 pos = hme.child(`node`).child(`transform`).vec3_(`pos`);
		vec3 scl = hme.child(`node`).child(`transform`).vec3_(`scl`);
		writefln("\tPosition: ", pos);
		writefln("\tScale: ", scl);
		with(hme.child(`node`)){
			with(child(`mesh`)){
				with(child(`verts`, 0, 1)){
					uint numVerts = count();
					writefln("\tVertices: ", numVerts);
					vec3[] vertices;
					vertices.alloc(numVerts);
					vec3_(vertices);
					foreach(inout v; vertices){
						v.x = v.x * scl.x * scale + pos.x;
						v.y = v.y * scl.y * scale + pos.y;
						v.z = v.z * scl.z * scale + pos.z;
					}
					mesh.vertices = vertices.dup;
					vertices.free();
					if(numVerts<100)
						writefln("\tVertices: ", mesh.vertices);
				}
				with(child(`faces`, 0, 1)){
					uint numFaces = count();
					writefln("\tFaces: ", numFaces);
					vec3i[] faces;
					faces.alloc(numFaces);
					vec3i_(faces);
					foreach(f;faces){
						mesh.indices.length = mesh.indices.length + 3;
						mesh.indices[$-3] = f.x;
						mesh.indices[$-2] = f.y;
						mesh.indices[$-1] = f.z;
					}
					faces.free();
					if(numFaces < 100)
						writefln("\tIndices: ", mesh.indices);
				}
			}
		}
		writefln("Loaded.");

	}

	Mesh mesh;
	float scale = 1.0f;
}
